//const indexedDB = indexedDB || mozIndexedDB || webkitIndexedDB || msIndexedDB;
/**
 * IndexDB数据库对象
 * @class
 * @memberof w
*/
class IndexDB {
  constructor(name, version) {
      this.name = name;
      this.version = version;
      this.db = null;
  }
  open(callback) {
      let request = indexedDB.open(this.name, this.version);
      request.onupgradeneeded = event => {
          this.db = event.target.result;
          this.db.createObjectStore("data", {
              keyPath: "id"
          });

      }
      request.onsuccess = event => {
          this.db = event.target.result;
          callback(this)
      };
      request.onerror = event => {
          console.error("数据库打开失败");
      };
      return request;
  }
  openPromise() {
      return new Promise((resolve, reject) => {
          let request = indexedDB.open(this.name, this.version);
          request.onupgradeneeded = event => {
              this.db = event.target.result;
              this.db.createObjectStore("data", {
                  keyPath: "id"
              });

          }
          request.onsuccess = event => {
              this.db = event.target.result;
              resolve(this)
          };
          request.onerror = event => {
              reject(`error-数据库初始化失败`);
          };
      })
  }
  /**
   * 异步方式添加数据
   * @method
   * @param {Object} data 
   * @example
   * indexdb.addPromise({id:ss,value}).then(()=>{});
   * //或
   * await indexdb.addPromise({id:ss,value});
   */
  addPromise(data) {
      return new Promise((resolve, reject) => {
          let transaction = this.db.transaction(['data'], 'readwrite');
          let objectStore = transaction.objectStore('data');
          let request = objectStore.add(data);
          request.onsuccess = event => {
              event.result = event.target.result;
              resolve(event)
          };
          request.onerror = event => {
              event.result = event.target.result;
              reject(event)
          };
      });
  }
  //{id:ss,value}
  add(data, callback) {
      let transaction = this.db.transaction(['data'], 'readwrite');
      let objectStore = transaction.objectStore('data');
      let request = objectStore.add(data);
      request.onsuccess = event => {
          if (callback) {
              event.result = event.target.result;
              callback(event)
          }
      };
      request.onerror = event => {
          if (callback) {
              event.result = event.target.result;
              callback(event)
          }
      };
  }
  putPromise(data) {
      return new Promise((resolve, reject) => {
          let transaction = this.db.transaction(['data'], 'readwrite');
          let objectStore = transaction.objectStore('data');
          let request = objectStore.put(data);
          request.onsuccess = event => {
              event.result = event.target.result;
              resolve(event)
          };
          request.onerror = event => {
              event.result = event.target.result;
              reject(event)
          };
      })
  }
  put(data, callback) {
      let transaction = this.db.transaction(['data'], 'readwrite');
      let objectStore = transaction.objectStore('data');
      let request = objectStore.put(data);
      request.onsuccess = event => {
          if (callback) {
              event.result = event.target.result;
              callback(event)
          }
      };
      request.onerror = event => {
          if (callback) {
              event.result = event.target.result;
              callback(event)
          }
      };
  }
  /**
   * 从数据库中查询数据项。
   * @method
   * @param {String} key 数据项主键。
   */
  getPromise(key) {
      return new Promise((resolve, reject) => {
          let transaction = this.db.transaction(['data'], 'readonly');
          let objectStore = transaction.objectStore('data');
          let request = objectStore.get(key);
          request.onsuccess = function (event) {
              event.result = event.target.result;
              resolve(event)
          };
          request.onerror = function (event) {
              event.result = event.target.result;
              reject(event)
          };
      })
  }
  get(key, callback) {
      let transaction = this.db.transaction(['data'], 'readonly');
      let objectStore = transaction.objectStore('data');
      let request = objectStore.get(key);
      request.onsuccess = function (event) {
          if (callback) {
              event.result = event.target.result;
              callback(event)
          }
      };
      request.onerror = function (event) {
          if (callback) {
              event.result = event.target.result;
              callback(event)
          }
      };
      return request;
  }
  /**
   * 从数据库中删除数据项。
   * @method
   * @param {String} key 数据项主键。
   */
  removePromise(key) {
      return new Promise((resolve, reject) => {
          let transaction = this.db.transaction(['data'], 'readwrite');
          let objectStore = transaction.objectStore('data');
          let request = objectStore.delete(key);
          request.onsuccess = function (event) {
              event.result = event.target.result;
              resolve(event)
          };
          request.onerror = function (event) {
              event.result = event.target.result;
              reject(event)
          };
      })
  }
  remove(key, callback) {
      let transaction = this.db.transaction(['data'], 'readwrite');
      let objectStore = transaction.objectStore('data');
      let request = objectStore.delete(key);
      request.onsuccess = function (event) {
          if (callback) {
              event.result = event.target.result;
              callback(event)
          }
      };
      request.onerror = function (event) {
          if (callback) {
              event.result = event.target.result;
              callback(event)
          }
      };
      return request;
  }
}
export { IndexDB }